/*
* Copyright @ 2014 com.iflysse.trains
* manger_v2.0 上午8:54:03
* All right reserved.
*
*/
package com.iflysse.servlet.column;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iflysse.pojo.ColumnInfo;
import com.iflysse.service.ColumnInfoService;
import com.iflysse.utils.PagerBean;

/**
 * @desc: manger_v2.0
 * @author: wql
 * @createTime: 2014年11月20日 上午8:54:03
 * @history:
 * @version: v1.0
 */
@WebServlet("/column/columnadmin.do")
public class ColumnInfoServlet extends HttpServlet {
	private ColumnInfoService service = new ColumnInfoService();
	/*
	*(non-Javadoc)
	* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	/*
	*(non-Javadoc)
	* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	*/
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String mtd=req.getParameter("method");
		
		if("load".equals(mtd)){
			load(req, resp);
		}else if("del".equals(mtd)){
			delColumn(req, resp);
		}else if("initForm".equals(mtd)){
			initForm(req, resp);
		}else if("addForm".equals(mtd)){
			addForm(req, resp);
		}else if("insert".equals(mtd)){
			insert(req, resp);
		}else if("update".equals(mtd)){
			update(req, resp);
		}else{
			req.getRequestDispatcher("/WEB-INF/views/columnadmin/columnadmin.jsp").forward(req, resp);
		}
	}
	/**
	*
	* @author: wql
	* @createTime: 2014年11月28日 上午9:11:24
	* @history:
	* @param req
	* @param resp void
	 * @throws IOException 
	 * @throws ServletException 
	*/
	private void addForm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ColumnInfo columninfo=new ColumnInfo();
		req.setAttribute("columninfo", columninfo);
		req.getRequestDispatcher("/WEB-INF/views/columnadmin/columnindex.jsp").forward(req, resp);		
	}

	/**
	*
	* @author: wql
	* @createTime: 2014年11月20日 上午10:45:00
	* @history:
	* @param req
	* @param resp void
	 * @throws IOException 
	 * @throws ServletException 
	*/
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ColumnInfo column = new ColumnInfo();
		column.setId(Integer.parseInt(req.getParameter("id")));
		column.setStatus(req.getParameter("status"));
		column.setName(req.getParameter("name"));
		column.setAlias(req.getParameter("alias"));
		column.setType(req.getParameter("type"));
		column.setDesciption(req.getParameter("desciption"));
		column.setLogo(req.getParameter("logo"));
		column.setSeotitle(req.getParameter("seotitle"));
		column.setSeotitle(req.getParameter("seotitle"));
		column.setKw(req.getParameter("kw"));
		service.updatetcolumn(column);
		req.getRequestDispatcher("/WEB-INF/views/columnadmin/newcolumnindex.jsp").forward(req, resp);		
	}

	/**
	*
	* @author: wql
	* @createTime: 2014年11月20日 上午10:44:58
	* @history:
	* @param req
	* @param resp void
	 * @throws IOException 
	 * @throws ServletException 
	*/
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ColumnInfo column = new ColumnInfo();
		column.setId(Integer.parseInt(req.getParameter("id")));
		column.setStatus(req.getParameter("status"));
		column.setName(req.getParameter("name"));
		column.setAlias(req.getParameter("alias"));
		column.setType(req.getParameter("type"));
		column.setDesciption(req.getParameter("desciption"));
		column.setLogo(req.getParameter("logo"));
		column.setSeotitle(req.getParameter("seotitle"));
		column.setSeotitle(req.getParameter("seotitle"));
		column.setKw(req.getParameter("kw"));
		service.insertcolumn(column);
		req.getRequestDispatcher("/WEB-INF/views/columnadmin/columnadmin.jsp").forward(req, resp);		
		
	}

	/**
	*
	* @author: wql
	* @createTime: 2014年11月20日 上午10:44:55
	* @history:
	* @param req
	* @param resp void
	 * @throws IOException 
	 * @throws ServletException 
	*/
	private void initForm(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String id=req.getParameter("columnid");
		int columnid=Integer.parseInt(id);
		ColumnInfo columninfo=null;
		if(columnid>0){
			columninfo=service.findColumnById(columnid);
		}
		else{
			columninfo=new ColumnInfo();
		}
//		System.out.println(columninfo);
		req.setAttribute("columninfo", columninfo);
		req.getRequestDispatcher("/WEB-INF/views/columnadmin/newcolumnindex.jsp").forward(req, resp);
	}

	/**
	*
	* @author: wql
	* @createTime: 2014年11月20日 上午10:44:53
	* @history:
	* @param req
	* @param resp void
	 * @throws IOException 
	*/
	private void delColumn(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String ids=req.getParameter("columnids");
		String[] idsArr= ids.split(","); 
		for(int i=0;i<idsArr.length;i++){
			int id = Integer.parseInt(idsArr[i]);
			service.delColumnById(id);
		}
		print2Page(resp, "{\"msg\":\"删除成功\"}");		
	}

	/**
	*
	* @author: wql
	* @createTime: 2014年11月20日 上午10:44:50
	* @history:
	* @param req
	* @param resp void
	 * @throws IOException 
	*/
	public void load(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String search = req.getParameter("searchword");
		if(search.equals("")){
			PagerBean<ColumnInfo> pager=new PagerBean<ColumnInfo>();
			int page=1,pageSize=10;
			String mysort="id";
			String myorder="asc";
			
			try{
				page =Integer.parseInt(req.getParameter("page"));
				pageSize =Integer.parseInt(req.getParameter("rows"));
				
				if(req.getParameter("sort")!=null&&req.getParameter("order")!=null)
				{
					mysort= req.getParameter("sort");
					myorder= req.getParameter("order");	
				}
			}catch(NumberFormatException ex){
				
			}
			pager.setMysort(mysort);
			pager.setMyorder(myorder);
			pager.setPage(page);
			pager.setPageSize(pageSize);
			pager=service.findColumnPager(pager);
			print2Page(resp, pager.toString());
		}
		else{
			List<ColumnInfo> columns= new ArrayList<ColumnInfo>();
			columns=service.findColumnByName(search);
			print2Page(resp, columns.toString());
		}	
		
		
	}

	/**
	*
	* @author: wql
	* @createTime: 2014年11月20日 上午10:51:50
	* @history:
	* @param resp
	* @param string void
	 * @throws IOException 
	*/
	private void print2Page(HttpServletResponse resp,String json) throws IOException {
		resp.setContentType("application/json;charset=UTF-8");
		Writer writer=resp.getWriter();
		writer.write(json);
		writer.flush();
	}
	
	
	/**
	*根据当前的栏目对象，获取当前栏目对象在此集合中的子栏目对象,并对源集合进行元素减少操作
	* @author: Rongyu
	* @createTime: 2014年11月21日 上午8:37:47
	* @history:
	* @param srclist
	* @param info
	* @return List<ColumnInfo>
	
	public List<ColumnInfo> getChildenColumnById(List<ColumnInfo> srclist,ColumnInfo info)
	{
		List<ColumnInfo> childenList=new ArrayList<ColumnInfo>();
		for(int i=0;i<srclist.size();i++)
		{
			if(info.getId()==srclist.get(i).getPpid())
			{
				childenList.add(srclist.get(i));
				srclist.remove(i);
				i--;
			}
		}
		return childenList;
	}
	public List<ColumnInfo> getDstColumnOrdered(List<ColumnInfo> srcList)
	{
		List<ColumnInfo> dstList=new ArrayList<ColumnInfo>();
		List<ColumnInfo> tempList=null;
		for(int i=0;i<srcList.size();i++)
		{
			tempList=getChildenColumnById(srcList,srcList.get(i));
			srcList.get(i).setName("+"+srcList.get(i).getName());
			dstList.add(srcList.get(i));
			if(tempList.size()!=0)
			{
				for(int j=0;j<tempList.size();j++)
				{
					tempList.get(j).setName("&emsp;&emsp;|----"+tempList.get(j).getName());
					dstList.add(tempList.get(j));
				}
			}
			srcList.remove(i);
			i--;
		}
		return dstList;
		
	}
	*/

	
	

}
